﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using log4net;
using log4net.Config;

[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]


namespace Work1_UI
{
    public partial class Form1 : Form
    {
        //public Form1()
        //{
        //    InitializeComponent();
        //}


        public static Form1 form1;

        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;//跨线程非法访问属性设置为false
            form1 = this;
        }

        public static readonly ILog log = LogManager.GetLogger(typeof(Form1)); //日志对象

        public enum MessageType
        {
            Debug,
            Info,
            Error,
            Warn
        }

        //信息显示函数
        public void MessageShow(MessageType messageType, string msg2)
        {
            int i = this.dataGridView1.Rows.Add();
            this.dataGridView1.Rows[i].Cells[0].Value = System.DateTime.Now.ToString("MM.dd-HH:mm");
            this.dataGridView1.Rows[i].Cells[1].Value = messageType;
            this.dataGridView1.Rows[i].Cells[2].Value = msg2;
        }

        //选择图像文件
        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread1 = new Thread(() =>
            {
                SelectPicture selectPicture1 = new SelectPicture();
            });
            thread1.SetApartmentState(ApartmentState.STA); //将当前线程设置为单线程单元(STA)模式
            thread1.Start();
        }

        //选择图像文件夹
        private void button2_Click(object sender, EventArgs e)
        {
            Thread thread2 = new Thread(() =>
            {
                SelectFile selectFile1 = new SelectFile();
            });
            thread2.SetApartmentState(ApartmentState.STA); //将当前线程设置为单线程单元(STA)模式
            thread2.Start();
        }

        //删除选中图像
        private void button3_Click(object sender, EventArgs e)
        {


            if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
            {
                const string message = "是否要连本地文件一并删除?";
                const string caption = "删除选中文件";
                var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                // If the no button was pressed ...
                if (result == DialogResult.No) //不删除本地
                {
                    for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
                    {
                        int index = 0; //记录选中的图片序数
                        index = this.listView1.SelectedItems[0].ImageIndex;
                        Form1.form1.imageList1.Images[index].Dispose(); //销毁imageList对应图片
                        ListViewItem item = listView1.SelectedItems[i];
                        listView1.Items.Remove(item); //删除listView1对应项

                    }
                    form1.MessageShow(MessageType.Info, "删除成功");
                    log.Info("删除成功");
                }
                else if (result == DialogResult.Yes) //同时删除本地
                {
                    try
                    {
                        for (int i = 0; i < listView1.SelectedItems.Count; i++)
                        {
                            File.Delete(listView1.SelectedItems[i].Text); //删除本地文件

                            int index = 0; //记录选中的图片序数
                            index = this.listView1.SelectedItems[0].ImageIndex;
                            Form1.form1.imageList1.Images[index].Dispose(); //销毁imageList对应图片
                            ListViewItem item = listView1.SelectedItems[i];
                            listView1.Items.Remove(item); //删除listView1对应项

                            form1.MessageShow(MessageType.Info, "本地删除成功");
                            log.Info("本地删除成功");
                        }
                    }
                    catch
                    {
                        form1.MessageShow(MessageType.Error, "本地删除失败");
                        log.Error("本地删除失败");
                    }
                }
            }
        }

        //删除所有图像
        private void button4_Click(object sender, EventArgs e)
        {
            if (imageList1.Images.Count != 0)
            {
                const string message = "是否要连本地文件一并删除?";
                const string caption = "删除全部文件";
                var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.No) //不删除本地
                {
                    listView1.Items.Clear(); //清空所有项
                    imageList1.Images.Clear();
                    form1.MessageShow(MessageType.Info, "删除所有图像成功");
                    log.Info("删除所有图像成功");
                }
                else //删除本地
                {
                    try
                    {
                        for (int i = 0; i < listView1.Items.Count; i++)
                        {
                            File.Delete(listView1.Items[i].Text);
                        }

                        imageList1.Images.Clear();
                        listView1.Items.Clear();

                        form1.MessageShow(MessageType.Info, "本地删除成功");
                        log.Info("本地删除成功");
                    }
                    catch
                    {
                        form1.MessageShow(MessageType.Error, "本地删除失败");
                        log.Error("本地删除失败");
                    }
                }
            }
        }

        //所选图像另存为
        private void button5_Click(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count == 1)//判断listview有单个被选中项
            {
                int index = 0; //记录选中的图片序数
                //int index2 = 0;
                index = this.listView1.SelectedItems[0].ImageIndex;
                //index2 = this.listView1.SelectedItems[0].Index;  // 图片序数与项的序数并不同步

                SaveFileDialog savedialog = new SaveFileDialog();
                savedialog.Filter = "Jpg 图片|*.jpg|Bmp 图片|*.bmp|Png 图片|*.png";
                savedialog.FilterIndex = 0;
                savedialog.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录
                savedialog.CheckPathExists = true;//检查目录
                savedialog.FileName = System.DateTime.Now.ToString("yyyyMMddHHmmss") + "-"; ;//设置默认文件名
                if (savedialog.ShowDialog() == DialogResult.OK)
                {

                    string sourceFile = this.listView1.SelectedItems[0].Text;//根据路径名字复制图片
                    FileInfo file = new FileInfo(sourceFile);
                    if (file.Exists) //可以判断源文件是否存在
                    {
                        FileStream fileStream = new FileStream(this.listView1.SelectedItems[0].Text, FileMode.Open, FileAccess.Read); //使用流避免文件占用
                        Image image = Image.FromStream(fileStream);
                        image.Save(savedialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);

                        fileStream.Flush(); //清除缓冲区
                        fileStream.Close(); //关闭
                        fileStream.Dispose(); //释放
                    }

                    //Form1.form1.imageList1.Images[index].Save(savedialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);// 通过imageList保存 图像模糊
                    form1.MessageShow(MessageType.Info, "保存成功");
                    log.Info("保存成功");
                }
            }

        }

        //listView选择事件
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectitem = listView1.SelectedItems.Cast<ListViewItem>().FirstOrDefault();
            if (selectitem != null)
            {
                int index = 0; //记录选中的图片序数
                index = this.listView1.SelectedItems[0].ImageIndex;
                FileStream fileStream = new FileStream(this.listView1.SelectedItems[0].Text, FileMode.Open, FileAccess.Read); //使用流
                this.userControl11.SetImage(this.listView1.SelectedItems[0].Text, Image.FromStream(fileStream)); //自定义控件接口函数
                fileStream.Flush(); //清除缓冲区
                fileStream.Close(); //关闭
                fileStream.Dispose(); //释放
            }

        }
    }
}
